
use std::sync::Mutex;

static MY_SHARE: Mutex<u32> = Mutex::new(3);

fn poisoner() {
    let mut lock = MY_SHARE.lock().unwrap();
    *lock += 1;
    panic!("the poisoner strikes!")
}

fn main() {
    // let my_shared = Mutex::new(0);
    // let lock = MY_SHARE.lock().unwrap();
    // std::mem::drop(lock);
    // if let Ok(_lock) = MY_SHARE.try_lock() {
    //     println!("got the lock");
    // } else {
    //     println!("did not get the lock");
    // }
    let handle = std::thread::spawn(poisoner);
    println!("Trying to return from the thread");
    println!("{:?}", handle.join());
    
    let lock = MY_SHARE.lock();
    println!("{:?}", lock);

    let recovered_data = lock.unwrap_or_else(|poison| {
        println!("Recovered from poison");
        poison.into_inner()
    });
    println!("{:?}", recovered_data);
}
